package io.rocketman.serialize.kryo;

import com.esotericsoftware.kryo.Kryo;

/**
 * @author He Peng
 * @create 2018-05-26 16:23
 * @update 2018-05-26 16:23
 * @updatedesc : 更新说明
 * @see
 */
public class ThreadBindingKryo {

    private static final ThreadLocal<Kryo> KRYO_THREAD_LOCAL = new ThreadLocal<Kryo>() {
        @Override
        protected Kryo initialValue() {
            return new Kryo();
        }
    };

    public static Kryo get() {
        Kryo kryo = KRYO_THREAD_LOCAL.get();
        if (null == kryo) {
            set(newKryo());
        }
        return KRYO_THREAD_LOCAL.get();
    }

    public static void set(Kryo kryo) {
        KRYO_THREAD_LOCAL.set(kryo);
    }

    private static Kryo newKryo() {
        return new Kryo();
    }
}
